#include "i2c.h"
#include "at24x.h"
#include <stdio.h>
void AT24C64_Init(void)
{
    I2C_Init();
}
int AT24C64_WriteByte(uint16_t WordAddress, uint8_t Data)
{
    I2C_Start();
    I2C_Write(AT24CX_ADDRESS_WRITE);

    if (ACK != I2C_Wait_Ack())
    {
        printf("write device_w error\n");
        return -1;
    }

    I2C_Write(WordAddress<<8);
    if (ACK != I2C_Wait_Ack())
    {
        printf("write rom_add_H error\n");
        return -1;
    }
    I2C_Write(WordAddress);
    if (ACK != I2C_Wait_Ack())
    {
        printf("write rom_add_L error\n");
        return -1;
    }

    I2C_Write(Data);
    if (ACK != I2C_Wait_Ack())
    {
        printf("write data error\n");
        return -1;
    }
    I2C_Stop();
    printf("AT24C64_WriteByte ok\n");
    return 0;
}

int AT24C64_ReadByte(uint16_t  WordAddress,uint8_t  *Data)
{
    I2C_Start();
    I2C_Write(AT24CX_ADDRESS_WRITE);
    if (ACK != I2C_Wait_Ack())
    {
        printf("write device_w error\n");
        return -1;
    }
     I2C_Write(WordAddress<<8);
    if (ACK != I2C_Wait_Ack())
    {
        printf("write rom_add_H error\n");
        return -1;
    }
    I2C_Write(WordAddress);
    if (ACK != I2C_Wait_Ack())
    {
        printf("write rom_add_L error\n");
        return -1;
    }

    I2C_Start();
    I2C_Write(AT24CX_ADDRESS_READ);
    if (ACK != I2C_Wait_Ack())
    {
        printf("write device_ error\n");
        return -1;
    }
    *Data = I2C_Read();
    I2C_Send_Ack(0);
    I2C_Stop();
    return 0;
}
